package com.rykj.base;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rykj.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;


/**
 * description:
 * <p/>
 * Created by ycw on 2017/7/17.
 */
public class LoadingDialogFragment extends DialogFragment {

    private static final String TAG = "LoadingDialogFragment";

    private TextView tvMessage;
    private LinearLayout llRoot;

    private String tipString = "加载中...";

    public static LoadingDialogFragment newInstance() {
        Bundle args = new Bundle();
        LoadingDialogFragment fragment = new LoadingDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.black));
        colorDrawable.setAlpha(120);
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawable(colorDrawable);
        }
        getDialog().setCanceledOnTouchOutside(false);
        //        setCancelable(false);
        View view = inflater.inflate(R.layout.view_loading_dialog, null);
        tvMessage = (TextView) view.findViewById(R.id.tv_message);
        llRoot = (LinearLayout) view.findViewById(R.id.ll_root);
        return view;
    }

    public void showTip(FragmentManager manager, String tipStr) {
        this.tipString = tipStr;
        if (isAdded()) {
            if (tvMessage != null) {
                tvMessage.setText(tipString);
            }
        } else {
            super.show(manager, TAG);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (tvMessage != null) {
            tvMessage.setText(tipString);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
